import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
  state: {
    roles: [],
    userPermiss: [],
  },
  getters: {
    getRoles: state => state.roles,
    getPermiss: state => state.userPermiss,
  },
  mutations: {
    SET_ROLES(state, roles) {
      state.roles = roles
    },
    SET_PERMISS(state, userPermiss) {
      state.userPermiss = userPermiss
    },
    // 添加清除数据的 mutations
    CLEAR_ROLES(state) {
      state.roles = []
    },
    CLEAR_PERMISS(state) {
      state.userPermiss = []
    },
    // 可以添加一个清除所有用户数据的方法
    CLEAR_USER_DATA(state) {
      state.roles = []
      state.userPermiss = []
    }
  },
  actions: {
    setRoles({ commit }, roles) {
      commit('SET_ROLES', roles)
    },
    setPermiss({ commit }, userPermiss) {
      commit('SET_PERMISS', userPermiss)
    },
    // 添加清除数据的 actions
    clearRoles({ commit }) {
      commit('CLEAR_ROLES')
    },
    clearPermiss({ commit }) {
      commit('CLEAR_PERMISS')
    },
    // 添加一个清除所有用户数据的 action
    clearUserData({ commit }) {
      commit('CLEAR_USER_DATA')
    }
  },
  modules: {
  },
  plugins: [
    createPersistedState({
      key: 'my-vuex-store',
      paths: ['roles', 'userPermiss'],
      storage: window.localStorage,
    })
  ]
})